package com.sias.reflection.class_;

import com.sias.Cat;

/**
 * @author 吴文杰
 * @version 1.0
 * @createTime 2023-05-02 13:52:33
 *
 * 获取Class类对象
 */
@SuppressWarnings("all")
public class GetClass {
  public static void main(String[] args) throws Exception {
    //1.Class.forName()
    String classAllPath = "com.sias.Cat";
    Class<?> cls1 = Class.forName(classAllPath);
    System.out.println(cls1); //class com.sias.Cat


    //2.类名.class 多用于参数传递
    Class<Cat> cls2 = Cat.class;
    System.out.println(cls2); //class com.sias.Cat


    //3.通过 对象.getClass() 多用于有对象实例
    Cat cat = new Cat();
    Class<? extends Cat> cls3 = cat.getClass();
    System.out.println(cls3); //class com.sias.Cat


    //4.通过类加载器[4种]来获取类的Class对象
    //(1)先得到类加载器 cat
    ClassLoader classLoader = cat.getClass().getClassLoader();
    //(2)通过类加载器得到Class对象
    Class<?> cls4 = classLoader.loadClass(classAllPath);
    System.out.println(cls4);

    System.out.println(cls1.hashCode());
    System.out.println(cls2.hashCode());
    System.out.println(cls3.hashCode());
    System.out.println(cls4.hashCode());


    //5.基本数据类型(byte、short、int、long、float、double、boolean、char) 按如下方式得到Class对象
    Class<Integer> integerClass = int.class;
    Class<Character> characterClass = char.class;
    Class<Boolean> booleanClass = boolean.class;
    System.out.println(integerClass);

    //6.基本数据类型对应的包装类 可以通过 .TYPE 得到Class对象
    Class<Integer> type1 = Integer.TYPE;
    Class<Character> type2 = Character.TYPE;
    System.out.println(type1);
    System.out.println(integerClass.hashCode()+"\t"+type1.hashCode());
  }
}
